package com.zs.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.system.comm.util.RandomUtil;
import com.system.comm.util.Snowflake;
import com.system.comm.util.Tools;
import com.system.common.pojo.BisCouponActive;
import com.system.common.pojo.BisCouponRecord;
import com.system.common.pojo.SysUser;
import com.zs.service.BisCouponActiveService;

@Controller
@RequestMapping("active")
public class BisCouponActiveController {
	
	@Autowired
	private BisCouponActiveService bisCouponActiveService;
	SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@RequestMapping("queryActive")
	public String query(Model model,HttpServletRequest request,String activeId){
		Map<String, Object> map=QueryActive(request);
		List<Map<Object,Object>> activeList=bisCouponActiveService.queryBisCouponActive(map);
		model.addAttribute("activeList", activeList);
		return "BisShopUser/ShowBisCouponActive";
	}
	
	private Map<String, Object> QueryActive(HttpServletRequest request){
		Map<String, Object> map=new HashMap<>();
		String activeName=request.getParameter("activeName");
		String isDel=request.getParameter("isDel");
		if (activeName!=null&&!activeName.equals("")) {
			map.put("activeName", activeName);
			request.setAttribute("activeName", activeName);
		}
		if (isDel!=null&&!isDel.equals("")) {
			map.put("isDel", isDel);
			request.setAttribute("isDel", isDel);
		}
		return map;
	}
	
	@RequestMapping("SavaActive")
	public String sava(BisCouponActive bisCouponActive,HttpSession session){
		bisCouponActive.setActiveId(RandomUtil.randomString(8));
		try {
			bisCouponActive.setCreateTime(dateFormat.parse(dateFormat.format(new Date())));
			bisCouponActive.setModifyTime(dateFormat.parse(dateFormat.format(new Date())));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SysUser sysUser=(SysUser)session.getAttribute("user");
       if (sysUser!=null) {
           bisCouponActive.setCreateUserId(sysUser.getUserId());
           bisCouponActive.setModifyUserId(sysUser.getUserId());
	   }
		bisCouponActive.setIsDel("0");
		int recordNum=bisCouponActive.getBeautyNum()*bisCouponActive.getBondNum();
		Set<String> set=Tools.createCdkey(recordNum, 10);
		Iterator<String> iterator=set.iterator();
		while (iterator.hasNext()) {
			BisCouponRecord record=new BisCouponRecord();
			String cdkey =iterator.next();
			Snowflake snowflake=new Snowflake(0, 0);
			String num=Long.toString(snowflake.nextId());
			record.setRecordId(num.substring(0, num.length()-2));
			record.setActiveId(bisCouponActive.getActiveId());
			record.setCdkey(cdkey);
			record.setStatus("1");
			try {
				record.setCreateTime(dateFormat.parse(dateFormat.format(new Date())));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			bisCouponActiveService.savaRecord(record);
		}
		bisCouponActiveService.savaActive(bisCouponActive);
		return "redirect:/active/queryActive";
	}
	
	@RequestMapping("DelActive")
	public String delete(String activeId){
		String []id=activeId.split(",");
		for (String strid : id) {
			BisCouponActive bisCouponActive=new BisCouponActive();
			bisCouponActive.setActiveId(strid);
			bisCouponActive.setIsDel("1");
			bisCouponActiveService.editActive(bisCouponActive);
		    BisCouponRecord record=new BisCouponRecord();
		    record.setActiveId(strid);
		    record.setStatus("3");
		    bisCouponActiveService.editByRecord(record);
		}
		return "redirect:/active/queryActive";
	}
	
	@RequestMapping("queryById")
	public void querybyid(HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> map=mohuQuery(request);
		List<BisCouponRecord> RecordList=bisCouponActiveService.queryByActiveId(map);
		for (BisCouponRecord record : RecordList) {
			if (record.getMobile()==null||record.getMobile().equals("")) {
				record.setMobile("未知");
			}
		}
		try {
			ObjectMapper mapper=new ObjectMapper();
			String jsonStr=mapper.writeValueAsString(RecordList);
			response.setCharacterEncoding("utf-8");
			response.getWriter().write(jsonStr);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private Map<String, Object>  mohuQuery(HttpServletRequest request){
		Map<String, Object> map=new HashMap<>();
		String key=request.getParameter("key");
		String photo=request.getParameter("photo");
		String state=request.getParameter("state");
		String activeId=request.getParameter("activeId");
		System.out.println(activeId);
		map.put("activeId", activeId);
		if (key!=null&&!key.equals("")) {
			map.put("key", key);
		}
		if (photo!=null&&!photo.equals("")) {
			map.put("photo", photo);
		}
		if (state!=null&&!state.equals("")) {
			map.put("state", state);
		}
		return map;
	}
}
	
